package org.hxyjs.dao.impl;

import org.hxyjs.dao.SubjectDao;
import org.hxyjs.entity.Subject;
import org.hxyjs.mapper.SubjectMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("subjectDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class SubjectDaoImpl implements SubjectDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private SubjectMapper subjectMapper;
    public List<Subject>  getAllSubjects() throws Exception{
        return subjectMapper.selectByExample(null);
    }

    @Override
    public Integer deleteSubjectBySjno(String sjno)  throws Exception{
        return subjectMapper.deleteByPrimaryKey(sjno);
    }

    @Override
    public Integer updateSubjectBySjno(Subject subject)  throws Exception{
        return subjectMapper.updateByPrimaryKey(subject);
    }

    @Override
    public Integer addSubject(Subject subject)  throws Exception{
        subject.setSjno(initId());
        return subjectMapper.insertSelective(subject);
    }
    public String initId(){
        String maxSjno = subjectMapper.getMaxSjno();
        return GenNumTools.initId("",3,maxSjno);
    }
    @Override
    public List<Subject> getSubjectByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)subjectMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return subjectMapper.getSubjectByPage(map);
    }
}
